UpdateSchoolCommandHandler.execute   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 48
rs 9.16
c 0
b 0
f 0
cc 3
1
import { CommandHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { UpdateSchoolCommand } from './UpdateSchoolCommand';
4
import { ISchoolRepository } from 'src/Domain/School/Repository/ISchoolRepository';
5
import { IsSchoolAlreadyExist } from 'src/Domain/School/Specification/IsSchoolAlreadyExist';
6
import { SchoolAlreadyExistException } from 'src/Domain/School/Exception/SchoolAlreadyExistException';
7
import { SchoolNotFoundException } from 'src/Domain/School/Exception/SchoolNotFoundException';
8
9
@CommandHandler(UpdateSchoolCommand)
10
export class UpdateSchoolCommandHandler {
11
  constructor(
12
    @Inject('ISchoolRepository')
13
    private readonly schoolRepository: ISchoolRepository,
14
    private readonly isSchoolAlreadyExist: IsSchoolAlreadyExist
15
  ) {}
16
17
  public async execute(command: UpdateSchoolCommand): Promise<string> {
18
    const {
19
      id,
20
      reference,
21
      address,
22
      city,
23
      name,
24
      email,
25
      zipCode,
26
      numberOfClasses,
27
      numberOfStudents,
28
      status,
29
      type,
30
      observation,
31
      phoneNumber
32
    } = command;
33
34
    const school = await this.schoolRepository.findOneById(id);
35
    if (!school) {
36
      throw new SchoolNotFoundException();
37
    }
38
39
    if (
40
      reference !== school.getReference() &&
41
      true === (await this.isSchoolAlreadyExist.isSatisfiedBy(reference))
42
    ) {
43
      throw new SchoolAlreadyExistException();
44
    }
45
46
    school.update(
47
      reference,
48
      name,
49
      address,
50
      zipCode,
51
      city,
52
      status,
53
      type,
54
      email,
55
      phoneNumber,
56
      numberOfStudents,
57
      numberOfClasses,
58
      observation
59
    );
60
61
    await this.schoolRepository.save(school);
62
63
    return school.getId();
64
  }
65
}
66